package easy;

public class Solution_405 {

    public String toHex(int num) {
        if (num == 0) {
            return "0";
        }
        char[] chars = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        StringBuffer buffer = new StringBuffer();
        for (; num != 0; ) {
            int mod = num % 16;
            mod = (mod >= 0 ? mod : (16 + mod));
            num = num >>> 4;
            buffer.append(chars[mod]);
        }
        return buffer.reverse().toString();
    }

    public static void main(String[] args) {
        Solution_405 model = new Solution_405();
        System.out.println(model.toHex(16));
        System.out.println(model.toHex(26));
        System.out.println(model.toHex(-1));
    }

    public static String hex(int num) {
        StringBuffer buffer = new StringBuffer();
        while (num > 0) {
            buffer.append(num % 2);
            num /= 2;
        }
        return buffer.reverse().toString();
    }
}
